/*vuex 的 actions 模块*/
import {reqAddress, reqCategorys, reqShops} from '../api'
import {MODIFY_LOCATION, RECEIVE_ADDRESS, RECEIVE_CATEGORYS, RECEIVE_SHOPS} from './mutation-types'

export default {

  //修改redux中的经纬度
  async changeLocation({commit, state}) {
    let delay = setInterval( () => {
      console.log('执行了Interval');
      let location = {};
      location.latitude = window.Blatitude;
      location.longitude = window.Blongitude;
      console.log(location);
      commit(MODIFY_LOCATION, location);

      if (state.longitude !== 116.23456) {
        clearInterval(delay);
      }

    }, 500)
  },

  // 异步获取地址
  async getAddress({commit, state}) {
    const geohash = state.latitude + ',' + state.longitude;
    const result = await reqAddress(geohash);
    commit(RECEIVE_ADDRESS, {address: result.data})
  },
  // 异步获取分类列表
  async getCategorys({commit}) {
    const result = await reqCategorys();
    commit(RECEIVE_CATEGORYS, {categorys: result.data})
  },
  // 异步获取商家列表
  async getShops({commit, state}) {
    const {latitude, longitude} = state;
    const result = await reqShops({latitude, longitude});
    commit(RECEIVE_SHOPS, {shops: result.data})
  }
}
